#!/usr/bin/python 
# -*- coding=utf-8 -*-
'''
master server
'''
import logging
from ConfigParser import ConfigParser
from server import Server

class Master():
    def __init__(self, conf_path):
        # init configure
        try:
            self.conf = ConfigParser()
            self.conf.read(conf_path)
        except Exception, e:
            print 'read configure file failed, exit.', str(e)
            exit

        # init logger
        log_level = {}
        log_level['DEBUG'] = logging.DEBUG
        log_level['INFO'] = logging.INFO
        log_level['ERROR'] = logging.ERROR
        try:
            loglevel = log_level[self.conf.get('all', 'log-level')]
        except Exception, e:
            loglevel = logging.DEBUG

        formatstr = '%(asctime)s %(name)-8s %(levelname)-8s %(message)s'
        logging.basicConfig(level = loglevel, format = formatstr)
        self.logger = logging.getLogger('master')
        self.logger.info('logger init OK.')

        # init server
        self.server = Server(self.conf)

    def run(self):
        self.server.start()
        pass

if __name__ == '__main__':
    from server import Server
    master = Master('./conf/master.conf')
    master.run()
